import NodeUtil from "../../Script/framework/utils/NodeUtil";
import Game from "../../Script/Game/Game";
import Slot from "./Slot";
import { PetData } from "../../Script/Game/Entity/PetData"; 
import Bullet from "./Bullet";
import BattleView from "../../Script/Game/BattleView";

/**
 * 战机
 * 追踪怪物  搜
 * 索范围
 * 
 * 有限状态机
 * 
 * 搜索->行走->攻击
 * 
 * 出生->死亡
 * 
 * 
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class Fighter extends cc.Component {

    @property(sp.Skeleton)
    spine: sp.Skeleton = null;
  
    @property(cc.Node)
    bullet:cc.Node = null; 

    isBattle:boolean = false;
    isDrag:boolean = false;
 
    oldPos:cc.Vec2 = cc.v2();
    isStartAi : boolean = false;
    game:Game = null;
    beginClickPoine:cc.Vec3 = null;
    fighterData:PetData = null; 
    target:cc.Node;
    faceDir:number = 1;

    battleViewCom:BattleView = null;

    onLoad () {
       
    }

    reg(){ 
        this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnded, this);
    }

    unreg(){
        this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnded, this);
    }

    _onTouchBegan( e:cc.Event.EventTouch ){
        cc.Tween.stopAllByTarget(this.node);
        if(this.isStartAi){
           this.isDrag = true;
        }

        var delta = e.touch.getDelta();
        this.node.x += delta.x;
        this.node.y += delta.y;
        this.beginClickPoine = cc.v3(this.node.x,this.node.y,0);
    }

    _onTouchMoved( e:cc.Event.EventTouch ){  
        var delta = e.touch.getDelta();
        this.node.x += delta.x;
        this.node.y += delta.y; 
    }

    _onTouchEnded( e ){  
        this.isDrag = false; 
        let node:cc.Vec3 = this.node.convertToWorldSpaceAR(cc.v3(0,0,0)) 
        let slotComp:Slot = this.game.checkRecoverySlot(node);
        if(slotComp){ 
            //拖动的位置有战机
            if(slotComp.data){
                this.fighterData.inField = false;
                this.fighterData.idx = slotComp.idx;
                let temp = this.fighterData; 
                slotComp.data.inField  = true;
                slotComp.data.idx = -1;
                this.fighterData = slotComp.data;
                this.node.position = this.beginClickPoine; 
                this.updateSpine();

                slotComp.data = temp;
                slotComp.updateSlot(); 
            }else{
                slotComp.data = this.fighterData;
                slotComp.data.idx = slotComp.idx;
                slotComp.data.inField = false;
                slotComp.updateSlot(); 
                this.removeGoToBattle(); 
            }

        }else{ 
            //如果超出了战斗场景范围就回到刚拖动的点
            let fightScene = this.game.battleScene;
            let leftMax =  - fightScene.width/2 + this.node.width/2;
            let rightMax = fightScene.width/2 - this.node.width/2
            let topMax =  - fightScene.height/2 + this.node.height/2;
            let bottomMax =  fightScene.height/2- this.node.height/2;
            let mx = this.node.x;
            let my = this.node.y;
            mx = Math.max( mx, leftMax);
            mx = Math.min( mx, rightMax );
            my = Math.max( my, topMax );
            my = Math.min( my, bottomMax);
            this.node.x= mx;
            this.node.y = my;
            this.action();   
        } 
    } 

    start () {

    }

    //检查是否需要更新spine
    updateSpine(){

    }
 
    //设置攻击目标
    setEnemy(enemy){
        this.target  = enemy;
    }


    setData(option,data:PetData){  
        this.game = option.game;
        this.battleViewCom = this.game.battleScene.getComponent(BattleView);
        this.fighterData = option.data;
    } 
    playAni(name){
        this.spine.setAnimation(0,name,true);
    }

    //上阵
    addGoToBattle(){ 
        this.reg(); 
    }

     //下阵
    removeGoToBattle(){
        this.fighterData = null;
        this.unreg(); 
        GPool.put(this.node);
    }   
 
    //开始自动执行
    startAI(){ 
        this.isStartAi = true;  
        this.reg();
        this.action();
        this.schedule(this.shot,0.3)

    }

    stopAI(){ 
        this.unschedule(this.shot);
        this.unreg();
    }


    action(){
        if(this.isStartAi && this.target){ 
            //追上去
           let v2:cc.Vec2 = NodeUtil.getDir(this.node.getPosition(),this.target.getPosition());
           this.node.scaleX = v2.x;
           cc.tween(this.node).to(4,{position:cc.v3(this.target.x,this.target.y,0)}).start();
        }  
    }


    shot(){
        if(this.isDrag)return;
        GPool.get("Bullet").then((node:cc.Node)=>{ 
            let battleCom = node.getComponent(Bullet)
            battleCom.setFigher(this); 
            battleCom.setFaceDir(this.node.scaleX)
            let pos = NodeUtil.getRelativePosition(this.bullet, this.battleViewCom.BulletLayer);
            this.battleViewCom.addBulletLayer(battleCom,pos); 
        }); 

    }

     update (dt) {}
}
